package cn.school.stu.evaluation.service.impl;

import cn.school.common.exception.ApiException;
import cn.school.common.utils.SysUserUtils;
import cn.school.stu.evaluation.dao.StuEvaPlanDao;
import cn.school.stu.evaluation.entity.StuEvaPlan;
import cn.school.stu.evaluation.service.StuEvaPlanService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author: lx
 * @date: 2023/5/25 9:30
 * @description:
 */
@Service
public class StuEvaPlanServiceImpl extends ServiceImpl<StuEvaPlanDao, StuEvaPlan> implements StuEvaPlanService {

    @Autowired
    StuEvaPlanDao stuEvaPlanDao;

    @Override
    public int createStuEvaPlan(StuEvaPlan stuEvaPlan) {
        //更新
        if(stuEvaPlan.getId() != null){
            return this.baseMapper.updateById(stuEvaPlan);
        }else{
            //新增
            stuEvaPlan.setState("0");
            return this.baseMapper.insert(stuEvaPlan);
        }
    }

    @Override
    public IPage<StuEvaPlan> findStuEvaPlanPageList(Map<String, Object> queryParam) {
        int pageNum = (int)queryParam.get("pageNum");
        int pageSize = (int)queryParam.get("pageSize");
        String planName = (String)queryParam.get("planName");
        String tenantId = SysUserUtils.getSysUser().getTenantId();
        int count = stuEvaPlanDao.findStuEvaPlanCount(planName,tenantId);
        int startNum = (pageNum - 1) * pageSize;
        List<StuEvaPlan> list = stuEvaPlanDao.findStuEvaPlanPageList(startNum,pageSize,planName,tenantId);
        IPage<StuEvaPlan> page = new Page<>();
        page.setTotal(count);
        page.setRecords(list);
        return page;
    }

    @Override
    public StuEvaPlan findStuEvaPlanById(Long id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public int deleteStuEvaPlanById(Long id) throws ApiException{
        StuEvaPlan stuEvaPlan = this.baseMapper.selectById(id);
        if("1".equals(stuEvaPlan.getState())){
            throw new ApiException("应用中方案不可删除");
        }
        return this.baseMapper.deleteById(id);
    }
}
